# apps/devices/serializers.py
from rest_framework import serializers
from .models import Device
import uuid


class DeviceSerializer(serializers.ModelSerializer):
    # 让前端既能拿到 room_id，也能直接读 room.name
    room_id   = serializers.IntegerField(source='room.room_id', read_only=True)
    room_name = serializers.CharField(source='room.name', read_only=True)

    class Meta:
        model = Device
        fields = (
            'device_id',
            'device_name',
            'device_type',
            'room',       # 用于写入：前端提交 room 的 ID
            'room_id',    # 方便前端读取
            'room_name',  # 方便前端读取并分组
            'status',
            'control_value',
        )
        read_only_fields = ('device_id', 'room_id', 'room_name')

class DeviceControlSerializer(serializers.Serializer):
    action = serializers.CharField()
    value = serializers.IntegerField()

